<?php

namespace app\home\controller\business;

use app\common\controller\Home;

/**
 * 数据分析控制器
 */
class Data extends Home
{
    // 构造函数，继承父类
    public function __construct()
    {
        parent::__construct();

        // 全局模型加载
        $this->BusinessModel = model('Business.Business');

        $this->RecordModel = model('Business.Record');
    }
    public function index()
    {
        // 获取cookie
        $LoginAuth = cookie('LoginAuth') ? cookie('LoginAuth') : [];

        $busid = isset($LoginAuth['id']) ? $LoginAuth['id'] : 0;

        $business = $this->RecordModel->where(['busid' => $busid])->whereTime('createtime', 'm')->select();

        $business = collection($business)->toArray();

        $year = date('Y');

        $total = [];
        for ($i = 1; $i <= 12; $i++) {

            $data = $this->RecordModel->where(['busid' => $busid])->where('createtime', 'between time', [$year . '-' . $i . '-1', $year . '-' . $i . '-31'])->select();
            $data = collection($data)->toArray();

            $data = array_column($data,'total');

            $data = $data ? $data : 0;

            if($data)
            {
                $data=str_replace('-', '', $data);
                $data= array_sum($data);
            }
            $total[] = $data;
        }
        $total = json_encode($total,JSON_UNESCAPED_UNICODE);
        $this->view->assign([
            'total'=>$total,
        ]);

        // 渲染模板
        return $this->view->fetch();
    }
}
